package cn.weichuyu.snippet.comparejar.process;

import cn.weichuyu.snippet.comparejar.util.FileUtils;
import cn.weichuyu.snippet.comparejar.util.UnzipJar;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CompareExecutor {
    static Logger logger = Logger.getLogger(CompareExecutor.class);

    public void compareJar(String oldJarPath, String newJarPath, String workDirectory) {
        if (!workDirectory.endsWith(File.separator)) {
            workDirectory = workDirectory + File.separator;
        }
        String oldDestincationPath = "";
        String newDestincationPath = "";
        try {
            File f = new File(oldJarPath);
            f.getAbsoluteFile();
            oldDestincationPath = workDirectory + f.getName() + "_old";
            File f2 = new File(newJarPath);
            newDestincationPath = workDirectory + f2.getName() + "_new";
            //解压缩的代码很慢，还不知道怎么优化
            if (true) {
                //解压两个jar包到 工作目录
                logger.info("开始解压到路径 " + oldDestincationPath);
                UnzipJar.unzipJar(oldDestincationPath, oldJarPath);
                logger.info("开始解压到路径 " + newDestincationPath);
                UnzipJar.unzipJar(newDestincationPath, newJarPath);
            }
            logger.info("解压完成");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return;
        }

        //遍历两个jar包下所有文件，并算md5
        Map<String, String> oldMd5 = calculateMd5(oldDestincationPath);
        Map<String, String> newMd5 = calculateMd5(newDestincationPath);

        if (oldMd5 == null || oldMd5.isEmpty() || newMd5 == null || newMd5.isEmpty()) {
            logger.info("计算MD5失败");
            return;
        }
        //根据文件的相对路径，比较哪些 文件路径 在A包存在 但在B包不存在，哪些文件 路径一样，但MD5不一样。
        // 1-老的有，新的没有
        {
            System.out.println("1-老的有，新的没有");
            oldMd5.forEach((k, v) -> {
                if (!newMd5.containsKey(k)) {
                    System.out.println(k);
                }
            });
        }
        // 2-新的有，老的没有
        {
            System.out.println("2-新的有，老的没有");
            newMd5.forEach((k, v) -> {
                if (!oldMd5.containsKey(k)) {
                    System.out.println(k);
                }
            });
        }
        final String oldDestincationPath2 = oldDestincationPath;
        final String newDestincationPath2 = newDestincationPath;
        // 3-新老不一致
        {
            System.out.println("3-新老不一致");
            newMd5.forEach((k, v) -> {
                if (oldMd5.containsKey(k)) {
                    String oldMd5V = oldMd5.get(k);
                    if (!v.equals(oldMd5V)) {
                        System.out.println(oldDestincationPath2 + k);
                        System.out.println(newDestincationPath2 + k);
                        System.out.println("____________");
                    }
                }
            });
        }
    }

    private Map<String, String> calculateMd5(String folderPath) {
        Map<String, String> hashMap = new HashMap<>();
        File folder = new File(folderPath);
        List<String> absolutePathList = new ArrayList<String>();
        getFolderList(absolutePathList, folder);
        for (String path : absolutePathList) {
            String md5 = FileUtils.getMd5ByFile(path);
            String key = path.replace(folder.getAbsolutePath(), "");
            hashMap.put(key, md5);
            logger.info(key + ":" + md5);
        }
        return hashMap;
    }

    private void getFolderList(List<String> list, File folder) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            for (File f : files) {
                if (!f.isDirectory()) {
                    list.add(f.getAbsolutePath());
                } else {
                    getFolderList(list, f);
                }
            }
        } else {
            return;
        }
    }

}
